//应用实例
const app = getApp()

Page({
  data: {
    imgUrls: [
      'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
    ],
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    isShowInput: true,
    isNotShowInput: true,
    currDate: '2018-08-03',
    userBirth: '2018-08-03',
    radioValue: '男',
    openid: 'fail',
    userid: '',
    phone: '12345678912',
    birthday: '2000-01-01',
    sex: '男',
    weight: '180',
    inputLabel: '首次登录，请完善以下信息',
    submitLabel: '提交',
    phoneValue: '',
    checkedMale: true,
    checkedFemale: false,
    heightValue: '',
    signInLabel: '签到',
    isSignInBtnDisabled: false,
    totalScore: 0,
    username: app.globalData.USERNAME,
  },
  onLoad: function() {
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 10000,
      mask: true
    })
    //this.notShowInput();
    var that = this;
    that.getOpenId();
  },
  tapName: function(event) {
    var name = event.currentTarget.id;
    var url = '';
    switch (name) {
      case 'diary':
        url = '../diary/diary';
        break;
      case 'health':
        url = '../health/health';
        break;
      case 'kidney':
        url = '../kidney/kidney';
        break;
      case 'vedio':
        url = '../vedio/vedio';
        break;
      case 'tools':
        url = '../tools/tools';
        break;
      case 'friends':
        url = '../friends/friends';
        break;
    }
    if (url.length != 0) {
      wx.navigateTo({
        url: url,
      })
    }
    var that = this;
    if (name == 'headImage') {
      that.setData({
        inputLabel: '<返回',
        submitLabel: '修改信息'
      })
      //console.log("id:"+app.globalData.USERID)
      that.getUserData();
      that.showInput();
    }
    if (name == 'back' && that.data.inputLabel == '<返回') {
      //console.log(that.data.inputLabel)
      this.notShowInput();
    }
  },
  showInput() {
    var that = this;
    that.setData({
      isShowInput: true,
      isNotShowInput: false
    })
  },
  notShowInput() {
    var that = this;
    that.setData({
      isShowInput: false,
      isNotShowInput: true
    })
  },
  getOpenId: function() {
    var that = this;
    //console.log(this)
    wx.login({
      success: function(loginCode) {
        var appid = 'wx125ca331b58f7896'; //填写微信小程序appid
        var secret = '18417951223923c18e05266095495891'; //填写微信小程序secret
        //console.log(that)
        //调用request请求api转换登录凭证
        wx.request({
          url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret + '&grant_type=authorization_code&js_code=' + loginCode.code,
          header: {
            'content-type': 'application/json'
          },
          success: function(res) {
            //console.log(res)
            /*var that = this;*/
            that.setData({
              openid: res.data.openid
            })

            wx.request({
              url: app.globalData.URL + '/user/isFirstLogin',
              data: {
                openid: res.data.openid,
              },
              method: 'POST',
              header: {
                'content-type': 'application/json'
              },
              success: function(res) {
                //console.log(res)
                var backData = res;
                var isFirstLogin = res.data.result;
                if (res.data.id != -1) {
                  that.setData({
                    userid: res.data.id,
                    username: res.data.name,
                  })
                  app.globalData.USERID = res.data.id
                  app.globalData.USERNAME = res.data.name
                }
                //判断是否签到过
                that.isSigned();
                //获取用户信息
                //that.getUserData();
                //隐藏标签
                wx.hideToast()
                if (isFirstLogin) {
                  that.showInput();
                } else {
                  that.notShowInput();
                }
                // console.log(backData.data.result);

              },
            })
          }
        })
      }
    })
  },
  //提交
  userInfoSubmit: function(e) {
    var that = this;
    app.globalData.USERNAME = e.detail.value.username;
    that.setData({
      username: e.detail.value.username,
      birthday: e.detail.value.birth,
      height: e.detail.value.height,
      phone: e.detail.value.phoneNumber,
      sex: e.detail.value.sex
    });
    if (that.data.submitLabel == '提交') {
      wx.request({
        url: app.globalData.URL + '/user/firstLogin',
        data: {
          openid: that.data.openid,
          name: that.data.username,
          phone: that.data.phone,
          birthday: that.data.birthday,
          sex: that.data.sex,
          height: that.data.height,
        },
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        success: function(res) {
          console.log(res)
          that.setData({
            userid: res.data.id
          })

          app.globalData.USERID = res.data.id

          wx.showToast({
            title: '提交成功',
            icon: 'succes',
            duration: 1000,
            mask: true
          })
          that.notShowInput();
        },
      })
    } else {

      app.globalData.USERNAME = that.data.username;
      wx.request({
        url: app.globalData.URL + '/user/update',
        data: {
          name: that.data.username,
          id: app.globalData.USERID,
          phone: that.data.phone,
          birthday: that.data.birthday,
          sex: that.data.sex,
          height: that.data.height,
        },
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        success: function(res) {
          console.log(res)
          wx.showToast({
            title: '修改成功',
            icon: 'succes',
            duration: 1000,
            mask: true
          })
          that.notShowInput();
        },
      })
    }


    //console.log(e)
  },
  //出生年月修改
  bindDateChange: function(e) {
    //console.log(e.detail.value)
    this.setData({
      userBirth: e.detail.value
    })
  },
  //单选框事件
  radioChange: function(e) {
    var that = this;
    that.setData({
      radioValue: e.detail.value
    })
    //console.log(that.data.radioValue)
  },
  //签到
  signInSubmit: function() {
    console.log("签到")
    var that = this;
    wx.request({
      url: app.globalData.URL + '/score/signIn',
      data: {
        uid: app.globalData.USERID,
        cause: app.globalData.SCORECAUSE[0],
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        if (res.data.result) {
          that.setData({
            signInLabel: '已签到',
            totalScore: res.data.totalScore,
            isSignInBtnDisabled: true,
          })
          wx.showToast({
            title: '签到成功',
            icon: 'succes',
            duration: 1000,
            mask: true
          })
        }
      },
    })
  },
  //是否签到
  isSigned: function() {
    var that = this;
    wx.request({
      url: app.globalData.URL + '/score/isSigned',
      data: {
        uid: app.globalData.USERID,
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        //console.log(res)
        that.setData({
          totalScore: res.data.totalScore,
        })
        if (res.data.result) {
          that.setData({
            signInLabel: '已签到',
            
            isSignInBtnDisabled: true,
          })
        }
      },
    })
  },
  //获取用户信息
  getUserData: function() {
    var that = this;
    wx.request({
      url: app.globalData.URL + '/user/edit',
      data: {
        id: app.globalData.USERID,
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        //console.log(res);
        that.setData({
          username: res.data.name,
          userBirth: res.data.birthday,
          phoneValue: res.data.phone,
          heightValue: res.data.height,
          checkedMale: res.data.sex == '男',
          checkedFemale: res.data.sex == '女',
        })
      }
    })
  }
})